/*
 * Generated on Apr 17, 2005
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
package cgl.narada.service.record;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;

/**
 * @author hbulut
 * 
 * TODO To change the template for this generated type comment go to Window -
 * Preferences - Java - Code Style - Code Templates
 */
public class RecordResponseEvent implements RecordResponse {

  private boolean hasRequestIdentifier = false;
  private String  requestIdentifier    = null;
  private int     entityId             = 0;
  private int     templateId           = 0;
  private boolean success              = false;
  boolean         hasAdditionalInfo    = false;
  private String  additionalInfo       = null;

  private String  moduleName           = "RecordResponseEvent: ";

  public RecordResponseEvent(byte[] marshalledBytes) {
    ByteArrayInputStream baInputStream = new ByteArrayInputStream(
        marshalledBytes);
    DataInputStream din = new DataInputStream(baInputStream);
    try {
      din.readByte();
      /** RecordServiceExchange.RECORD_SERVICE_RESPONSE */

      hasRequestIdentifier = din.readBoolean();
      if (hasRequestIdentifier) {
        byte[] idBytes = new byte[din.readInt()];
        din.readFully(idBytes);
        requestIdentifier = new String(idBytes);
      }

      entityId = din.readInt();
      templateId = din.readInt();

      success = din.readBoolean();

      hasAdditionalInfo = din.readBoolean();
      if (hasAdditionalInfo) {
        byte[] infoBytes = new byte[din.readInt()];
        din.readFully(infoBytes);
        additionalInfo = new String(infoBytes);
      }
    } catch (IOException ioe) {
      System.out.println(moduleName + "Error while un-marshalling");
      ioe.printStackTrace();
    }
    /** end try-catch block */

  }

  public byte[] getBytes() {
    ByteArrayOutputStream baOutputStream = new ByteArrayOutputStream();
    DataOutputStream dout = new DataOutputStream(baOutputStream);
    byte[] marshalledBytes = null;
    try {
      dout.writeByte(RecordServiceExchange.RECORD_SERVICE_RESPONSE);
      /** Deal with request identifier */
      dout.writeBoolean(hasRequestIdentifier);
      if (hasRequestIdentifier) {
        byte[] idBytes = requestIdentifier.getBytes();
        dout.writeInt(idBytes.length);
        dout.write(idBytes);
      }

      dout.writeInt(entityId);
      dout.writeInt(templateId);

      dout.writeBoolean(success);

      dout.writeBoolean(hasAdditionalInfo);
      if (hasAdditionalInfo) {
        byte[] objBytes = additionalInfo.getBytes();
        dout.writeInt(objBytes.length);
        dout.write(objBytes);
      }
      marshalledBytes = baOutputStream.toByteArray();
    } catch (IOException ioe) {
      System.out.println(moduleName + "Error while marshalling");
    }
    /** end try-catch block */

    return marshalledBytes;
  }

  public RecordResponseEvent(String requestIdentifier, int entityId,
      int templateId, boolean success, String additionalInfo) {

    this.requestIdentifier = requestIdentifier;
    if (this.requestIdentifier != null) {
      this.hasRequestIdentifier = true;
    }

    this.entityId = entityId;
    this.templateId = templateId;
    this.success = true;
    this.additionalInfo = additionalInfo;
    if (this.additionalInfo != null) {
      this.hasAdditionalInfo = true;
    }
  }

  public String toString() {
    String stringRep = moduleName;
    stringRep += "\nhasRequestIdentifier=" + hasRequestIdentifier;
    stringRep += "\nRequestIdentifier=" + requestIdentifier;
    stringRep += "\nEntityId=" + entityId;
    stringRep += "\nTemplateId=" + templateId;

    stringRep += "\nsuccess=" + success;

    stringRep += "\nhasAdditionalInfo=" + hasAdditionalInfo;
    stringRep += "\nadditionalInfo=" + additionalInfo;
    return stringRep;
  }

  public String getAdditionalInfo() {
    return additionalInfo;
  }

  public int getEntityId() {
    return entityId;
  }

  public boolean hasAdditionalInfo() {
    return hasAdditionalInfo;
  }

  public boolean hasRequestIdentifier() {
    return hasRequestIdentifier;
  }

  public String getModuleName() {
    return moduleName;
  }

  public String getRequestIdentifier() {
    return requestIdentifier;
  }

  public boolean isSuccess() {
    return success;
  }

  public int getTemplateId() {
    return templateId;
  }

  public static void main(String[] args) {
    RecordResponseEvent res = new RecordResponseEvent("/RequestIdentifier", 23,
        34, true, "just for testing");
    System.out.println("req:\n" + res.toString());
    System.out.println("\n****************************\n");
    RecordResponseEvent res2 = new RecordResponseEvent(res.getBytes());
    System.out.println("req2:\n" + res2.toString());

  }
}